package com.kingint.web.portal.data.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kingint.mbg.bas.entity.BasHotel;
import com.kingint.mbg.data.entity.DataHouseServices;
import com.kingint.mbg.data.entity.DataRoomHygiene;
import com.kingint.mbg.data.mapper.DataHouseServicesMapper;
import com.kingint.mbg.dto.data.DataBackParam;
import com.kingint.mbg.dto.data.DataFrontBack;
import com.kingint.mbg.dto.data.DataParam;
import com.kingint.mbg.sys.entity.UmsMember;
import com.kingint.web.admin.base.service.BasHotelService;
import com.kingint.web.portal.data.service.DataHouseServicesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kingint.web.sys.service.RedisService;
import com.kingint.web.sys.service.UmsAdminService;
import com.kingint.web.util.GetDateUtils;
import com.kingint.web.vo.CountParam;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 住客服务数据表 服务实现类
 * </p>
 *
 * @author wxh
 * @since 2021-01-23
 */
@Service
public class DataHouseServicesServiceImpl extends ServiceImpl<DataHouseServicesMapper, DataHouseServices> implements DataHouseServicesService {

    @Resource
    private RedisService redisService;
    @Resource
    private UmsAdminService adminService;
    /**
     * 添加住客服务数据
     * @param backParam
     * @return
     */
    @Override
    public Integer addDataHouseInfo(DataBackParam backParam) {
        //构成key
        Date date = new Date();
        String key = backParam.getHotelId() + "_" + GetDateUtils.getHour(date) + "_" + backParam.getName();
        String value = backParam.getRoomId() + "_" + backParam.getRoomName();
        Long lpush = redisService.lpush(key, value);
        return lpush.intValue();
    }

    @Override
    public Integer addDataHouseInfo(Integer hotelId, String countName, Integer roomId, String roomName) {
        DataBackParam backParam = new DataBackParam();
        backParam.setHotelId(hotelId);
        backParam.setName(countName);
        backParam.setRoomId(roomId);
        backParam.setRoomName(roomName);
        return addDataHouseInfo(backParam);
    }

    @Resource
    private BasHotelService basHotelService;

    //定时1小时查询redis的服务工单，并写入数据库统计
    //住客服务涉及服务工单类型1，3，6
    @Scheduled(cron = "0 0 0/1 * * ?")
    private void timingSynchronization(){
        //查询所有酒店id
        List<BasHotel> list = basHotelService.list(null);

        list.forEach(v->{
            Date date = new Date();
            String key = v.getId()+"_"+ GetDateUtils.getHour(date)+"_"+ CountParam.SERVICECOUNT;
            List<String> lrang1 = redisService.lrang(key+"_1", 0, -1); //当前酒店当前小时所有维修工单
            List<String> lrang2 = redisService.lrang(key+"_3", 0, -1); //当前酒店当前小时所有服务工单
            List<String> lrang3 = redisService.lrang(key+"_6", 0, -1); //当前酒店当前小时所有清洁工单
            Map<String,Integer> countmap1 = null;
            //将redis中这个小时的数据按内容分类放入map
            lrang1.forEach(k->{
                if (ObjectUtils.isEmpty(countmap1.get(k))) countmap1.put(k,1);
                else countmap1.put(k,countmap1.get(k)+1);
            });

            countmap1.forEach((k,m)->{
                DataHouseServices dataCustomerPreference = new DataHouseServices();
                dataCustomerPreference.setTotalNum(m);
                dataCustomerPreference.setCreateTime(date);
                dataCustomerPreference.setHotelId(v.getId());
                dataCustomerPreference.setType(1);
                dataCustomerPreference.setRoomId(Integer.parseInt(k.split("_")[0]));
                dataCustomerPreference.setRoomName(k.split("_")[1]);
                this.save(dataCustomerPreference);
            });

            Map<String,Integer> countmap2 = null;
            lrang2.forEach(k->{
                if (ObjectUtils.isEmpty(countmap2.get(k))) countmap2.put(k,1);
                else countmap2.put(k,countmap2.get(k)+1);
            });

            countmap2.forEach((k,m)->{
                DataHouseServices dataCustomerPreference = new DataHouseServices();
                dataCustomerPreference.setTotalNum(m);
                dataCustomerPreference.setCreateTime(date);
                dataCustomerPreference.setHotelId(v.getId());
                dataCustomerPreference.setType(1);
                dataCustomerPreference.setRoomId(Integer.parseInt(k.split("_")[0]));
                dataCustomerPreference.setRoomName(k.split("_")[1]);
                this.save(dataCustomerPreference);
            });

            Map<String,Integer> countmap3 = null;
            lrang3.forEach(k->{
                if (ObjectUtils.isEmpty(countmap3.get(k))) countmap3.put(k,1);
                else countmap3.put(k,countmap3.get(k)+1);
            });

            countmap3.forEach((k,m)->{
                DataHouseServices dataCustomerPreference = new DataHouseServices();
                dataCustomerPreference.setTotalNum(m);
                dataCustomerPreference.setCreateTime(date);
                dataCustomerPreference.setHotelId(v.getId());
                dataCustomerPreference.setType(1);
                dataCustomerPreference.setRoomId(Integer.parseInt(k.split("_")[0]));
                dataCustomerPreference.setRoomName(k.split("_")[1]);
                this.save(dataCustomerPreference);
            });

        });

    }


    @Override
    public DataFrontBack getDataFrontBackInfo(){
        UmsMember currentMember = adminService.getCurrentMember();
        int j = 0;
        Integer hotelId = currentMember.getHotelId();//酒店id
        int hour = GetDateUtils.getHour(); //当前小时
        Integer num = 0;//当天总数
        Integer tnum = 0;//当小时总数
        String[] x1 = new String[24]; //时间坐标x轴
        Integer[] y1 = new Integer[24]; //坐标y轴
        Integer[] y2 = new Integer[24]; //坐标y轴
        Integer[] y3 = new Integer[24]; //坐标y轴

        String[] x4 = new String[24]; //房间坐标x轴
        Integer[] y4 = new Integer[24]; //坐标y轴

        String[] x5 = new String[24]; //房间坐标x轴
        Integer[] y5 = new Integer[24]; //坐标y轴

        String[] x6 = new String[24]; //房间坐标x轴
        Integer[] y6 = new Integer[24]; //坐标y轴
        Date date = new Date();
        String key = hotelId+"_"+ GetDateUtils.getHour(date)+"_"+ CountParam.SERVICECOUNT;
        List<String> lrang1 = redisService.lrang(key+"_1", 0, -1); //当前酒店当前小时所有维修工单
        List<String> lrang2 = redisService.lrang(key+"_3", 0, -1); //当前酒店当前小时所有服务工单
        List<String> lrang3 = redisService.lrang(key+"_6", 0, -1); //当前酒店当前小时所有清洁工单
        Map<String,Integer> countmap1 = null;

        //将redis中这个小时的数据按内容分类放入map
        lrang1.forEach(k->{
            if (ObjectUtils.isEmpty(countmap1.get(k))) countmap1.put(k,1);
            else countmap1.put(k,countmap1.get(k)+1);
        });
        tnum += lrang1.size();
        j=0;
        for (String k : countmap1.keySet()) {
            x4[j] = k;
            y4[j] = countmap1.get(k);
        }

        Map<String,Integer> countmap2 = null;
        lrang2.forEach(k->{
            if (ObjectUtils.isEmpty(countmap2.get(k))) countmap2.put(k,1);
            else countmap2.put(k,countmap2.get(k)+1);
        });
        tnum += lrang2.size();
        j=0;
        for (String k : countmap2.keySet()) {
            x5[j] = k;
            y5[j] = countmap2.get(k);
        }

        Map<String,Integer> countmap3 = null;
        lrang3.forEach(k->{
            if (ObjectUtils.isEmpty(countmap3.get(k))) countmap3.put(k,1);
            else countmap3.put(k,countmap3.get(k)+1);
        });
        tnum += lrang3.size();
        j=0;
        for (String k : countmap3.keySet()) {
            x6[j] = k;
            y6[j] = countmap3.get(k);
        }


        QueryWrapper<DataHouseServices> wrapper = new QueryWrapper<>();
        wrapper.eq("hotel_id",hotelId).ge("create_time", GetDateUtils.getTimeString(GetDateUtils.FORMAT_SHORT));
        List<DataHouseServices> list = this.list(wrapper);
        num = tnum;
        j = 0;
        for (DataHouseServices d :
                list) {
            num += d.getTotalNum();
            x1[j] = GetDateUtils.getHour(d.getCreateTime())+"";
            switch (d.getType()){
                case 1:
                    y1[j] = d.getTotalNum();
                    break;
                case 2:
                    y2[j] = d.getTotalNum();
                    break;
                case 3:
                    y3[j] = d.getTotalNum();
                    break;
            }
        }

        //拼装数据
        DataFrontBack dataFrontBack = new DataFrontBack();
        dataFrontBack.setNum1(BigDecimal.valueOf(num)); //当天总和数量
        dataFrontBack.setNum2(BigDecimal.valueOf(tnum));//当前小时总数
        dataFrontBack.setNum3(BigDecimal.valueOf(num/hour));//当前小时总和时间
        //构建坐标
        DataParam dataParam = new DataParam();
        dataParam.setTime(x1);
        dataParam.setValue(y1);
        dataParam.setValue1(y2);
        dataParam.setValue2(y3);

        DataParam dataParam1 = new DataParam();
        dataParam1.setTime(x4);
        dataParam1.setValue(y4);

        DataParam dataParam2 = new DataParam();
        dataParam1.setTime(x5);
        dataParam1.setValue(y5);

        DataParam dataParam3 = new DataParam();
        dataParam1.setTime(x6);
        dataParam1.setValue(y6);
        List<DataParam> list1 = new ArrayList<>();
        list1.add(dataParam);
        list1.add(dataParam1);
        list1.add(dataParam2);
        list1.add(dataParam3);
        dataFrontBack.setList(list1);//折线图

        return dataFrontBack;
    }
}
